package com.ynny.librarysystemmanager.controller;

import com.ynny.librarysystemmanager.common.R;
import com.ynny.librarysystemmanager.common.ServerReturnCode;
import com.ynny.librarysystemmanager.entity.BookErrorReturnCode;
import com.ynny.librarysystemmanager.entity.RecordErrorReturnCode;
import com.ynny.librarysystemmanager.entity.UserErrorReturnCode;
import com.ynny.librarysystemmanager.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/record")
public class RecordController {


    @Autowired
    RecordService recordService;

    @PostMapping("/{userId}/{bookId}")
    public R borrowBook(@PathVariable("userId") Integer userId,
                        @PathVariable("bookId") Integer bookId){

        Integer res = recordService.borrowerBook(userId, bookId);

        if (res.equals(UserErrorReturnCode.USER_NOT_FOUND)){
            return R.fail(UserErrorReturnCode.USER_NOT_FOUND , "用户不存在");
        } else if (res.equals(UserErrorReturnCode.USER_IS_DELETE)) {
            return R.fail(UserErrorReturnCode.USER_IS_DELETE , "用户状态不可用");
        } else if (res.equals(UserErrorReturnCode.USER_IS_NOT_ALLOW)) {
            return R.fail(UserErrorReturnCode.USER_IS_NOT_ALLOW , "用户无权限操作");
        } else if (res.equals(BookErrorReturnCode.BOOK_NOT_FOUND)) {
            return R.fail(BookErrorReturnCode.BOOK_NOT_FOUND , "图书不存在");
        } else if (res.equals(BookErrorReturnCode.BOOK_IS_DELETE)) {
            return R.fail(BookErrorReturnCode.BOOK_IS_DELETE , "图书不可用或下架");
        } else if (res.equals(BookErrorReturnCode.BOOK_IS_NOT_STORAGE)) {
            return R.fail(BookErrorReturnCode.BOOK_IS_NOT_STORAGE , "图书库存不足");
        } else if (res.equals(RecordErrorReturnCode.RECORD_REPEAT)) {
            return R.fail(RecordErrorReturnCode.RECORD_REPEAT , "不允许重复借阅同一本书");
        } else if (res.equals(ServerReturnCode.ERROR)){
            return R.fail(ServerReturnCode.ERROR , "服务器忙，请稍后再试");
        } else {
            return R.ok(bookId);
        }


    }
}
